// pages/coupon/mydetail.js
import {Model} from './model.js';
var model= new Model();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:'',
    company:'',
    workman:'',
    remark:'',
    modal: false,
    actions: [
      {
        name: '取消'
      },
      {
        name: '确认',
        color: '#ed3f14',
        loading: false
      }
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.options = options;
    model.getUserCouponInfo(options.id,(res)=>{
      var resdata=res.data;
      var data={};
      data.data=resdata.request;
      data.user_coupon_info=resdata.data;
      this.setData(data)
    })
  },

  clickTab: function (e) {
    var index = e.detail;
    console.log(e)
    if (index == 0) {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }

    if (index == 3) {
      this.goClickUse();
    }
  },

  // 使用
  goClickUse(){
    this.setData({modal:true})
  },
  // 地址
  address(event){
    this.setData({ address: event.detail.detail.value });
  },
  // 施工单位
  company(event){
    this.setData({ company: event.detail.detail.value });
  },
  // 负责人
  workman(event){
    this.setData({ workman: event.detail.detail.value });
  },
  // 备注
  remark(event){
    this.setData({ remark: event.detail.detail.value });
  },
  // 确认使用优惠券
  couponUse(event){
    var index = event.detail.index;
    if(index==0){
      this.setData({modal:false})
      return ;
    }
    var address=this.data.address;
    var company = this.data.company;
    var workman = this.data.workman;
    var remark = this.data.remark;
    if(address.length<=0){
      wx.showToast({
        title: '施工地址必填', icon:'none'
      })
      return ;
    }
    if (company.length <= 0) {
      wx.showToast({
        title: '施工单位必填', icon: 'none'
      })
      return;
    }
    if (workman.length <= 0) {
      wx.showToast({
        title: '施工负责人必填', icon: 'none'
      })
      return;
    }
    var param={
      id:this.options.id,
      address:address,
      company:company,
      workman:workman,
      remark:remark
    }
    model.useUserCoupon(param,(res)=>{
      var statusCode = res.statusCode;
      if (statusCode==200){
        if(res.data.data.code==1){
          wx.showToast({
            title:res.data.data.msg, icon: 'none'
          })
        }
      }else{
        wx.showToast({
          title: res.data.msg, icon:'none'
        })
      }

      setTimeout(function () {
        wx.reLaunch({
          url: '/pages/coupon/my',
        })
      }, 2000)
    })
  }
 
})